﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

namespace TabSaver
{
    [Serializable()]
    [XmlRoot("BrowserSession")]
    public class BrowserSession : ISerializable
    {
        public BrowserSession()
        {
            SessionEntries = new List<TabSession>();
        }
        public BrowserSession(SerializationInfo info, StreamingContext ctxt)
        {
            this.SessionEntries = (List<TabSession>)info.GetValue("SessionEntries", typeof(List<TabSession>));
        }

        public void AddTab(TabSession Tab)
        {
            SessionEntries.Add(Tab);
        }

        [System.Xml.Serialization.XmlArray("SessionEntries")]
        [System.Xml.Serialization.XmlArrayItem("TabSession", typeof(TabSession))]
        public List<TabSession> SessionEntries;

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("SessionEntries", this.SessionEntries);
        }
    }

    [Serializable()]
    public class TabSession : ISerializable
    {
        public TabSession()
        {
            TabTravelLogEntries = new List<TabSessionEntry>();
        }
        public TabSession(SerializationInfo info, StreamingContext ctxt)
        {
            this.TabTravelLogEntries = (List<TabSessionEntry>)info.GetValue("TabTravelLogEntries", typeof(List<TabSessionEntry>));
        }

        public void AddTravelLog(TabSessionEntry Entry)
        {
            TabTravelLogEntries.Add(Entry);
        }

        [System.Xml.Serialization.XmlArray("TabTravelLogEntries")]
        [System.Xml.Serialization.XmlArrayItem("TabSessionEntry", typeof(TabSessionEntry))]
        public List<TabSessionEntry> TabTravelLogEntries;

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("TabTravelLogEntries", this.TabTravelLogEntries);
        }
    }

    [Serializable()]
    public class TabSessionEntry : ISerializable
    {
        public TabSessionEntry(string URL, string Name)
        {
            this.URL = URL;
            this.Name = Name;
        }
        public TabSessionEntry()
        {
        }
        public TabSessionEntry(SerializationInfo info, StreamingContext ctxt)
        {
            this.URL = (string)info.GetValue("URL", typeof(string));
            this.Name = (string)info.GetValue("Name", typeof(string));
        }

        [XmlAttribute("URL")]
        public string URL { get; set; }
        [XmlAttribute("Name")]
        public string Name { get; set; }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("URL", this.URL);
            info.AddValue("Name", this.Name);
        }
    }


    public class Binder : SerializationBinder
    {
        public override Type BindToType(string assemblyName, string typeName)
        {
            Type tyType = null;

            string sShortAssemblyName = assemblyName.Split(',')[0];
            Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (Assembly ayAssembly in ayAssemblies)
            {
                if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
                {
                    tyType = ayAssembly.GetType(typeName);
                    break;
                }
            }

            return tyType;
        }
    }

}
